package com.base.cn.platform.os.controller.user.gradeconfig;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.user.gradeconfig.CusUserGradeConfig;
import com.base.cn.platform.os.service.user.gradeconfig.CusUserGradeConfigBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Data 等级
 * @ClassName  CusUserGradeConfigController
 * @author :ll
 * @Create Date : 2018年4月17日
 */
@RestController
@RequestMapping("/manage/user")
public class CusUserGradeConfigController extends BaseController {

    @Autowired
    private CusUserGradeConfigBiz cusUserGradeConfigBiz;

    /**
     * 分页搜索等级列表
     * @param currentPage 分页
     * @return
     */
    @RequestMapping("/queryCusUserGradeConfigList")
    public PageInfo<CusUserGradeConfig> queryUserList(@RequestParam(value = "currentPage",defaultValue = "1",required = false) int currentPage){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        return cusUserGradeConfigBiz.queryUserGradeConfigPage(page);
    }

    /**
     * 添加等级
     * @param cusUserGradeConfig
     * @return
     */
    @RequestMapping("/saveCusUserGradeConfig")
    public Map<String,Object> saveUser(@RequestBody CusUserGradeConfig cusUserGradeConfig){
        Map<String,Object> _map = checkUserGradeConfig(cusUserGradeConfig);
        if (!ObjectUtils.isNotEmpty(_map)){
            cusUserGradeConfig=cusUserGradeConfigBiz.addUpdateGradeConfig(cusUserGradeConfig);
            return ResultUtil.SUCCESS("操作成功",cusUserGradeConfig);
        }
        return _map;
    }

    /**
     * 搜索所有
     * @return
     */
    @RequestMapping("/queryCusUserGradeConfigAll")
    public List<CusUserGradeConfig> queryCusUserGradeConfigAll(){
        return cusUserGradeConfigBiz.find("1=1",null,null);
    }
    /**
     * 搜索根据Id
     * @return
     */
    @RequestMapping("/queryGradeConfigById")
    public CusUserGradeConfig queryGradeConfigById(@RequestParam("id") BigDecimal id){
        return cusUserGradeConfigBiz.findById(id);
    }

    //================================================================
    /**
     * 验证添加修改是否合理
     * @param userGradeConfig
     * @return
     */
    private Map<String,Object> checkUserGradeConfig(CusUserGradeConfig userGradeConfig){
        if(ObjectUtils.isNotEmpty(userGradeConfig)){
            if(StringUtils.isEmpty(userGradeConfig.getGradeTitle())){
                return ResultUtil.ERROR("等级头衔不能为空");
            }
            if(!"LV1".equals(userGradeConfig.getGradeName())&&
                    ObjectUtils.isNotEmpty(userGradeConfig.getExperience())&&userGradeConfig.getExperience()==0){
                return ResultUtil.ERROR("等级经验不能为0");
            }
        }
        return null;
    }

}
